/**
 * Title :	Property view
 * Description: Property editor
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package bsys.propertyview;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import jFamilyTree.commons.Utils;

/**
 * Text theDialog
 * 
 * @author Bernard Bou
 */
public class TextDialog extends JDialog
{
	private static final long serialVersionUID = 1L;

	/**
	 * Ok result
	 */
	private boolean ok;

	// C O M P O N E N T S

	/**
	 * Action on ok
	 */
	private ActionListener theOkAction;

	/**
	 * Action on cancel
	 */
	private ActionListener theCancelAction;

	/**
	 * Text area
	 */
	private JTextArea theTextArea;

	/**
	 * Constructor
	 * 
	 * @param thisOkAction
	 *        ok action
	 * @param thisCancelAction
	 *        cancel action
	 */
	public TextDialog(ActionListener thisOkAction, ActionListener thisCancelAction)
	{
		super();
		theOkAction = thisOkAction;
		theCancelAction = thisCancelAction;

		initialize();
	}

	/**
	 * Initialize
	 * 
	 * @throws Exception
	 */
	protected void initialize()
	{
		setTitle(Messages.getString("TextDialog.0")); //$NON-NLS-1$
		setResizable(true);

		// text
		theTextArea = new JTextArea();
		theTextArea.setLineWrap(true);
		theTextArea.setToolTipText(Messages.getString("TextDialog.1")); //$NON-NLS-1$
		JScrollPane thisScrollPane = new JScrollPane(theTextArea);
		thisScrollPane.setPreferredSize(new Dimension(300,100));

		// buttons
		JButton thisOKButton = new JButton(Messages.getString("TextDialog.2")); //$NON-NLS-1$
		JButton thisCancelButton = new JButton(Messages.getString("TextDialog.3")); //$NON-NLS-1$

		JPanel thisButtonPanel = new JPanel();
		thisButtonPanel.setLayout(new FlowLayout());
		thisButtonPanel.add(thisCancelButton);
		thisButtonPanel.add(thisOKButton);

		// event handling
		thisOKButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent thisEvent)
			{
				ok = true;
				theOkAction.actionPerformed(thisEvent);
				setVisible(false);
			}
		});
		thisCancelButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent thisEvent)
			{
				theCancelAction.actionPerformed(thisEvent);
				setVisible(false);
			}
		});

		// assemble
		JPanel thisPanel = new JPanel();
		thisPanel.setLayout(new BorderLayout());
		thisPanel.add(thisScrollPane, BorderLayout.CENTER);
		thisPanel.add(thisButtonPanel, BorderLayout.SOUTH);

		setContentPane(thisPanel);
	}

	/**
	 * Set text
	 * 
	 * @param thisText
	 *        text
	 */
	public void setText(String thisText)
	{
		theTextArea.setText(thisText);
		theTextArea.setCaretPosition(0);
	}

	/**
	 * Get text
	 * 
	 * @return text
	 */
	public String getText()
	{
		if (ok)
			return theTextArea.getText();
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setVisible(boolean)
	 */
	public void setVisible(boolean thisFlag)
	{
		if (thisFlag)
		{
			ok = false;

			pack();
			Utils.center(this);
		}
		super.setVisible(thisFlag);
	}
}
